package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.item.client.ItemClientFeign;
import com.atguigu.gmall.model.product.SkuInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Author: lx
 * @Time: 16:11
 * @Description:
 */
@Controller
public class ItemController {

    @Autowired
    private ItemClientFeign itemClientFeign;

    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable(name = "skuId") Long skuId, Model model) {
        Map<String, Object> resultMap = itemClientFeign.getItem(skuId);//Feign
        //判断  LinkHashMap not cast to SkuInfo
        LinkedHashMap<String,Object> skuInfo =
                (LinkedHashMap<String, Object>) resultMap.get("skuInfo");
        if(null != skuInfo){
            if(skuInfo.get("id") == null){
                return "redirect:首页?/d";//缓存穿透 京东的转发首页
            };
        }
    /*    SkuInfo skuInfo = (SkuInfo) resultMap.get("skuInfo");
        if (skuInfo.getId() == null) {
            return "redirect:首页?/d";
        }*/
        model.addAllAttributes(resultMap);
        //MVC 设计模式
        return "item/index";
    }


}
